import { request } from "../../utils/http";

Page({
  data: {
    id: "",
    result: "",
    sign_str: "",
  },
  onLoad(query) {
    let options = my.getEnterOptionsSync() || query;

    let queryStr = decodeURIComponent(options.query.query)
    let arg = queryStr.split('&')
    let queryObj = {}
    arg.forEach(item => {
      let i = item.split('=')
      queryObj[i[0]] = i[1]
    })
    let id = queryObj.order_id;
    let result = queryObj.result;
    console.log(id, result, queryStr, arg, options, 'canshu')
    // let id = 2432;
    console.log('3333')

    this.setData({id:id,},
      () => {
        console.log('1111')
        this.getDetail();
      }
    );
  },
  getDetail() {
    console.log('222')

    let id = this.data.id;
    request("/ant_new/sign_result/" + id, "get", {}).then((res) => {
      console.log(res);
      if (res.code == 200) {
        let status = res.data.ant_new_sign_status;
        let result;
        if (status == "FINISH") {
          result = 1;
        } else if (status == -1) {
          result = 2;
        }
        if (result == 1) {
          this.getNext();
        }
        this.setData(
          {
            result,
          },
          () => {}
        );
      }
    });
  },
  getNext() {
    request("/ant_new/create_withhold/" + this.data.id, "get", {}).then(
      (res) => {
        if (res.code == 200) {
          this.setData({
            sign_str: res.data.signStr,
            merchantId:res.data.merchantId,
            orderNo:res.data.orderId,
            tenantId:res.data.tenantId
          });
        }
      }
    );
  },
  next() {
    my.setStorageSync({
      data: this.data.id,
      key: 'blockchain_id'
    });
    my.navigateToMiniProgram({
      appId: "2021001152620480", // 固定值，无须修改。

      path: "pages/tripartite/index", // 固定值，无须修改。

      query: {
        signStr: this.data.sign_str, // 代扣签约创建接口返回的sign_str内容

        tenantId: this.data.tenantId, // 商家的用户code，在数科控制台中查询获取

        merchantId: this.data.merchantId, // 商户id,也是企业社会信用代码，保持与进件接口一致

        orderId: this.data.orderNo, // // 订单id，保持与进件接口一致

        redirectUrl:
          "alipays://platformapi/startapp?appId=2021004141663044&page=pages/blockchain-result/blockchain-result?order_id=" + this.data.id, // 代扣签署成功/失败回跳地址，示例 alipays://platformapi/startapp?appId=2021004108689505&page=pages/home/index，appId 和 page 需要根据实际情况调整真实数值
      }, // 需要携带的必要参数

      success: () => {},

      fail: () => {},
    });
  },
});
